<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="面向对象，定义类，POJO">
<meta property="og:type" content="article">
<meta property="og:title" content="Java日拱一卒--day4">
<meta property="og:url" content="http://yoursite.com/2020/03/28/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day4/index.html">
<meta property="og:site_name" content="很惭愧，就做了一点微小的工作">
<meta property="og:description" content="面向对象，定义类，POJO">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-03-27T16:43:03.000Z">
<meta property="article:modified_time" content="2020-07-01T13:54:12.421Z">
<meta property="article:author" content="文予">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/03/28/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day4/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Java日拱一卒--day4 | 很惭愧，就做了一点微小的工作</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">很惭愧，就做了一点微小的工作</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">念念不忘，必有回响</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/28/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day4/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/wenyu.png">
      <meta itemprop="name" content="文予">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="很惭愧，就做了一点微小的工作">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java日拱一卒--day4
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-03-28 00:43:03" itemprop="dateCreated datePublished" datetime="2020-03-28T00:43:03+08:00">2020-03-28</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-07-01 21:54:12" itemprop="dateModified" datetime="2020-07-01T21:54:12+08:00">2020-07-01</time>
              </span>

          
            <div class="post-description">面向对象，定义类，POJO</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="面向对象"><a href="#面向对象" class="headerlink" title="面向对象"></a>面向对象</h2><p>Java是面向对象的语言，提供了定义类、成员变量、方法等最基本的功能，类可以被认为是一种自定义的数据类型，可以用类来定义变量，所有用类定义出来的变量都是引用变量，他们将会引用到类的变量。类用于描述客观世界里某一类对象的共同特征，是对某一类对象的一种抽象。而对象则是类的具体存在，Java程序使用类的构造器来创造类的对象。</p>
<h3 id="定义类"><a href="#定义类" class="headerlink" title="定义类"></a>定义类</h3><p>两个重要概念：类<code>class</code>和对象<code>object</code>（也被称为实例，<code>instance</code>）。<br>Java里定义类的简单语法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[修饰符] <span class="class"><span class="keyword">class</span> 类名 //修饰符可以是<span class="title">public</span>，<span class="title">final</span>，<span class="title">abstract</span>，也可以省略</span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">//零到多个构造器定义</span></span><br><span class="line">    <span class="comment">//零到多个成员变量</span></span><br><span class="line">    <span class="comment">//零到多个方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>对于一个类定义而言，修饰符可以是<code>public</code>、<code>final</code>、<code>abstract</code>，或者完全省略这三个修饰符。类里面成员之间可以互相调用，但是<code>static</code>修饰 的成员不能访问没有<code>static</code>修饰的成员。</p>
<p>成员变量用于定义该类或者该类的实例所包含的状态数据，方法则用于定义该类或该类的实例的行为特征或者功能实现。构造器用于构造该类的实例，Java通过<code>new</code>关键字来调用构造器，从而返回该类的实例。例如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//学生类，变成对象（引用类型）</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;      <span class="comment">//通过类把学生实体共性抽象出来，这样java类就可以描述生活中学生情况</span></span><br><span class="line">    <span class="comment">//属性（访问设置），数据类型，属性都推荐使用包装类型</span></span><br><span class="line">    <span class="keyword">public</span> String name;</span><br><span class="line">    <span class="keyword">public</span> Integer age;</span><br><span class="line">    <span class="keyword">public</span> Boolean sex;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//方法（调用）</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">study</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"我爱学习"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"我爱锻炼身体"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>显而易见，在上面的代码中并没有编写构造器。Java语言会对没有编写构造器的类提供一个默认的构造器。<br>定义成员变量的语法格式如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[修饰符] 类型 成员变量名 [= 默认值];</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>修饰符</strong>可以省略，也可以是<code>public</code>、<code>protected</code>、<code>private</code>、<code>static</code>、<code>final</code>,其中<code>public</code>、<code>protected</code>、<code>private</code>三个最多只能出现其一，可以与<code>static</code>、<code>final</code>组合起来修饰成员变量。</li>
<li><strong>类型</strong>可以是Java愿意那种中的任何数据类型，包括基本类型和引用类型。</li>
<li><strong>成员变量名</strong>：驼峰规则</li>
<li><strong>默认值</strong>：定义成员变量的时候还可以指定一个可选的默认值。</li>
</ul>
<p>定义方法的语法格式如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[修饰符] 方法返回值类型  方法名(形参列表)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//若干条可执行语句组成的方法体</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p><strong>修饰符</strong>可以省略，也可以是<code>public</code>、<code>protected</code>、<code>private</code>、<code>static</code>、<code>final</code>、<code>abstract</code>，其中<code>public</code>、<code>protected</code>、<code>private</code>三个最多只能出现其中之一，<code>final</code>、<code>abstract</code>最多只能出现其中之一他们可以与<code>static</code>组合起来修饰方法。</p>
</li>
<li><p><strong>方法返回值类型</strong>：可以是Java允许的任何人数据类型，包括基本类型和引用类型；如果声明了方法返回值类型，则方法体内必须有一个有效的<code>return</code>语句，并且该语句返回一个与此处声明的类型匹配的变量或者表达式。另外，如果没有返回值，则必须用<code>void</code>来声明没有返回值。</p>
</li>
<li><p><strong>形参列表</strong>：用于定于该方法可以接受的参数，由若干组<code>参数类型 形参名</code>组成，以英文逗号<code>,</code>隔开。一旦在定义方法的时候指定了形参列表，则调用方法时必须传入对应的参数值：谁调用，谁负责赋值。</p>
</li>
</ul>
<p><code>static</code>是一个特殊的关键字，可用于修饰方法、成员变量等。<code>static</code>修饰的成员表明它属于这个类本身，而不是单个实例。注意静态成员不能直接访问非静态成员。因为静态成员属于类,不需要生成对象就存在了。</p>
<blockquote>
<p>有物混成，先天地生。</p>
</blockquote>
<p>而非静态需要生成对象才产生. 所以静态成员不能直接访问。</p>
<p>定义构造器的语法格式如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[修饰符] 构造器名 (形参列表)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//若干条可执行语句组成的构造器执行体</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>修饰符</strong>：可以省略，也可以是<code>public</code>、<code>protected</code>、<code>private</code>其中之一。</li>
<li><strong>构造器名</strong>：必须与类名相同</li>
<li><strong>形参列表</strong>：和定义方法形参列表的格式完全相同</li>
</ul>
<p>实际上据我理解，构造器之所以不能定义返回值类型，是因为构造器与类方法只有这个区别，一旦声明的返回值类型，构造器就变成类方法了。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Dog</span> </span>&#123;</span><br><span class="line">    <span class="comment">//创建默认构造方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Dog</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"这个叫做默认构造，也叫无参构造方法"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//有参构造方法，创建时就给小狗设置姓名</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Dog</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"有参构造方法1："</span>+name);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//构造方法可以有好多，可以有几个参数</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Dog</span><span class="params">(String name, Integer age)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"有参构造方法2："</span> + name +<span class="string">","</span>+ age);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//创建一个类的实例，这个类必须有默认构造方法，这个方法java会自动调用</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Dog dog1 = <span class="keyword">new</span> Dog();               <span class="comment">//=号后面的语句写的就是调用默认构造方法</span></span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"====================="</span>);</span><br><span class="line"></span><br><span class="line">        Dog dog2 = <span class="keyword">new</span> Dog(<span class="string">"小黑"</span>);   <span class="comment">//还会调用默认构造吗？还是直接调用有参构造呢？</span></span><br><span class="line"></span><br><span class="line">        Dog dog3 = <span class="keyword">new</span> Dog(<span class="string">"小白"</span>, <span class="number">5</span>);    <span class="comment">//它就直接找2个构造参数的方法</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="对象的产生"><a href="#对象的产生" class="headerlink" title="对象的产生"></a>对象的产生</h3><p>创建对象的根本途径是构造器，通过<code>new</code>关键字来调用某个类的构造器即可创建这个类的实例。例如</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用Person类定义一个Person类型的变量</span></span><br><span class="line">Person p;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 通过new关键字调用Person类的构造器，返回一个Person实例</span></span><br><span class="line"><span class="comment">//把Person实例赋值给p变量</span></span><br><span class="line">p = <span class="keyword">new</span> Person();</span><br></pre></td></tr></table></figure>

<p>实际上也可以简写成如下形式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Person p = <span class="keyword">new</span> Person();</span><br></pre></td></tr></table></figure>

<h3 id="POJO"><a href="#POJO" class="headerlink" title="POJO"></a>POJO</h3><p>POJO（Plain Ordinary Java Object）简单的Java对象，实际就是普通JavaBeans</p>
<ol>
<li>一堆私有成员变量，不是<code>public</code>修饰符，<code>private</code>修饰，私有（外部不能直接访问）</li>
<li>针对每个私有成员变量有一堆get和set方法</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> hello.day04.pojo;</span><br><span class="line"><span class="comment">//部门，POJO</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Dept</span> </span>&#123;</span><br><span class="line">    <span class="comment">//部门编号</span></span><br><span class="line">    <span class="keyword">private</span> Integer id;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//部门名称</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String deptName;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> hello.day04;</span><br><span class="line"><span class="keyword">import</span> day04.pojo.Dept;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestDept</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Dept dept = <span class="keyword">new</span> Dept();</span><br><span class="line">        <span class="comment">//访问公用属性</span></span><br><span class="line">        dept.deptName = <span class="string">"软件部"</span>;</span><br><span class="line">        System.out.println(dept.deptName);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//访问私有属性</span></span><br><span class="line">        dept.name = <span class="string">"软件部"</span>;</span><br><span class="line">        <span class="comment">//The field（属性，字段） Dept.name is not visible  </span></span><br><span class="line">        <span class="comment">//这个字段不可访问</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>修饰符：</p>
<ol>
<li>public        公用的，任何地方都能直接访问</li>
<li>private   私有的，不允许在类的外部进行访问。main自己Dept类里是可以访问，但是在其他类中就不能直接访问</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> hello.day04.pojo;</span><br><span class="line"></span><br><span class="line"><span class="comment">//部门，POJO</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Dept</span> </span>&#123;</span><br><span class="line">    <span class="comment">//POJO特点1：私有属性</span></span><br><span class="line">    <span class="keyword">private</span> Integer id;         <span class="comment">//部门编号</span></span><br><span class="line">    <span class="keyword">private</span> String name;            <span class="comment">//部门名称</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//POJO特点2：针对上面私有属性get和set方法</span></span><br><span class="line">    <span class="comment">//自动生成：鼠标右键，选择source菜单，选择getters和setters菜单</span></span><br><span class="line">    <span class="comment">//起到封装作用，get代表获取它的值，set代表设置它的值</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Integer <span class="title">getId</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"有人读取了这个密码"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(id==<span class="number">800</span>) &#123;       <span class="comment">//800编号是王思聪，它账务被冻结了，不允许转账</span></span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;       <span class="comment">//直接返回0，无法获取账务信息</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> id;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(Integer id)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"有人设置这个密码了"</span>);</span><br><span class="line">        <span class="keyword">this</span>.id = id;</span><br><span class="line">        System.out.println(<span class="string">"此人设置这个密码为："</span>+id);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> hello.day04.pojo;</span><br><span class="line"></span><br><span class="line"><span class="comment">//部门，POJO</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Dept</span> </span>&#123;</span><br><span class="line">    <span class="comment">//POJO特点1：私有属性</span></span><br><span class="line">    <span class="keyword">private</span> Integer id;         <span class="comment">//部门编号</span></span><br><span class="line">    <span class="keyword">private</span> String name;            <span class="comment">//部门名称</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//POJO特点2：针对上面私有属性get和set方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Integer <span class="title">getId</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> id;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(Integer id)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.id = id;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> hello.day04;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> day04.pojo.Dept;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestDept</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Dept dept = <span class="keyword">new</span> Dept();</span><br><span class="line"></span><br><span class="line">        dept.setId(<span class="number">800</span>);</span><br><span class="line">        dept.setName(<span class="string">"软件部"</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"部门编号："</span>+ dept.getId() );</span><br><span class="line">        System.out.println(<span class="string">"部门名称："</span> + dept.getName());</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/03/25/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day3/" rel="prev" title="Java日拱一卒--day3">
      <i class="fa fa-chevron-left"></i> Java日拱一卒--day3
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/03/28/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day5/" rel="next" title="Java日拱一卒-day5">
      Java日拱一卒-day5 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#面向对象"><span class="nav-number">1.</span> <span class="nav-text">面向对象</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#定义类"><span class="nav-number">1.1.</span> <span class="nav-text">定义类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#对象的产生"><span class="nav-number">1.2.</span> <span class="nav-text">对象的产生</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#POJO"><span class="nav-number">1.3.</span> <span class="nav-text">POJO</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="文予"
      src="/uploads/wenyu.png">
  <p class="site-author-name" itemprop="name">文予</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文予</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
